class Node(object):
    def __init__(self, val=None, children=None):
        self.val = val
        self.children = children

import collections
class Solution(object):
    def postorder(self, root):
        """
        :type root: Node
        :rtype: List[int]
        """
        if not root:
            return []
        stack = [root]
        a = collections.defaultdict(int)
        out = []
        while stack:
            node = stack[-1]
            if node.children and a[node] < len(node.children):
                stack.append(node.children[a[node]])
                a[node] += 1
            else:
                out.append(node.val)
                stack.pop()
        return out
